
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Mr Bao
 */
public class ListImageV2 extends Canvas implements Runnable, CommandListener {

    private int pointer = 0;
    private boolean isRunning = true;
    private Vector lstImages = new Vector();
    private HomeScreen homeScreen;
    private Command cmdBack;

    public ListImageV2(HomeScreen homeScreen) {
        this.homeScreen = homeScreen;
        cmdBack = new Command("Back", Command.BACK, 2);
        Common.Client.lstImages = new Vector();
        addCommand(cmdBack);
        setCommandListener(this);
        new Thread(this).start();
    }

    protected void paint(Graphics g) {
        Util.resetScreen(g, this);
        Util.drawTitle("Up Anh", g, this);
        drawListImage(g);
    }

    protected void keyPressed(int keyCode) {
        int key = getGameAction(keyCode);
        switch (key) {
            case DOWN:
                if (pointer - 4 > 0) {
                    pointer -= 4;
                    repaint();
                }
                break;
            case UP:
                if (pointer + 4 < lstImages.size() - 1) {
                    pointer += 4;
                    repaint();
                }
                break;
            case RIGHT:
                if (pointer < lstImages.size() - 1) {
                    pointer++;
                    repaint();
                }
                break;
            case LEFT:
                if (pointer > 0) {
                    pointer--;
                    repaint();
                }
                break;
            case FIRE:
//                homeScreen.DisplayViewImageForm((byte[]) lstImages.elementAt(pointer));
                break;
            default:
                break;
        }
    }

    private void drawListImage(Graphics g) {
        if (lstImages.size() > 0) {
            int y = Util.fontTitle.getHeight() + 5;
            for (int i = 0; i < lstImages.size(); i++) {
                try {
                    byte[] data = (byte[]) lstImages.elementAt(i);
                    Image image = Image.createImage(data, 0, data.length);
                    if (i == pointer) {
                        g.setColor(0xff0000);
                        g.drawRect((getWidth() / 4) * (i % 4), y + (getWidth() / 4) * (i / 4), getWidth() / 4 - 1, getWidth() / 4 - 1);
                        image = Util.resizeImage(image, getWidth() / 4, getWidth() / 4);
                        g.drawImage(image, (getWidth() / 4) * (i % 4), y + (getWidth() / 4) * (i / 4), 0);
                    } else {
                        image = Util.resizeImage(image, getWidth() / 4 - 4, getWidth() / 4 - 4);
                        g.drawImage(image, (getWidth() / 4) * (i % 4) + 2, y + (getWidth() / 4) * (i / 4) + 2, 0);
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

    public void run() {
        while (isRunning) {
            if (Common.Client.lstImages == null) {
                isRunning = false;
                return;
            }
            if (Common.Client.lstImages.size() > 0) {
                lstImages.addElement(Common.Client.lstImages.elementAt(0));
                Common.Client.lstImages.removeElementAt(0);
                repaint();
            }
            System.gc();
        }
    }

    public void commandAction(Command c, Displayable d) {
        if (c==cmdBack){
            homeScreen.DisplayChoseViewAlbumForm();
        }
    }
}
